<?php
namespace Home\Controller;
use Common\Model\LeavelModel;
use Common\Model\UserMoneyModel;
use Common\Model\CurrencyModel;
use Common\Model\UserModel;
use Common\Model\UserBankModel;
use Common\Api\Reward;
use Common\Model\FinanceLogModel;
use Common\Model\RegLogModel;
class TeamController extends HomeController{
    public function __construct()
    {
        parent::__construct();
    }
    public function register()
    {
        $bank_type=$this->getBankType();
        $this->assign('bank_type',$bank_type);

        $leavel_type=LeavelModel::newSelf()->getInfo();
        $this->assign('leavel_type',$leavel_type);
        
        $country=$this->getCountry();
        $this->assign('country',$country);
        
        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        $this->assign('user_money',$user_money);

        $currency=CurrencyModel::newSelf()->getInfo();
        $this->assign('currency',$currency);
        
        $this->display();
    }

    public function registerSub()
    {    	
        $agree=I('agree');
        $this->notEmptyValidate($agree,'请阅读会员注册协议');
        $this->eqValidate($this->config['CAN_REG'], 1,'暂时无法报单');
        $data['username']=rtrim(I('username'));
        $data['realname']=I('realname');
        $data['country']=(int)I('country');
        $data['phone']=I('phone');
        $data['email']=I('email');
        $data['tuiname']=I('tuiname');
        $data['jiename']=I('jiename');
        $data['quyu']=(int)I('quyu');
        $data['leavel']=I('leavel');
        $twopassword=I('twopassword');
//         $rs=checkPhoneCode(I('phone_code'),'reg',$data['phone']);
        
//         $this->trueValidate($rs,'手机验证码不正确');
        
        $this->notEmptyValidate($data['username'],'用户名不能为空');
        $this->notEmptyValidate($data['realname'],'真实姓名不能为空');
        $this->notEmptyValidate($data['country'],'请选择城市');
        $this->notEmptyValidate($data['phone'],'电话号码不能为空');
        $this->notEmptyValidate($data['email'],'邮箱不能为空');
        $this->notEmptyValidate($data['tuiname'],'推荐人不能为空');
        $this->notEmptyValidate($data['jiename'],'安置人不能为空');
        $this->notEmptyValidate($data['quyu'],'请选择安置位置');
        $this->notEmptyValidate($twopassword,'请填写二级密码');
        $this->notEmptyValidate($data['leavel'],'请选择会员等级');
		
        $leavel_type=LeavelModel::newSelf()->getInfo();
        $this->notEmptyValidate($leavel_type[$data['leavel']],'请正确选择会员等级');
        
        $need_currency=1;//账户余额
       
//         $this->notEmptyValidate($need_currency,'请选择开通组合');
        
        $model=UserModel::newSelf();
        $check=$model->checkUserTwoPassword($twopassword, $this->home_user_info['twopassword']);
        $this->trueValidate($check,'二级密码不正确');
        
        $user=$model->findUserByUsername($data['username']);
        $this->emptyValidate($user,'用户名已存在');

        $countPhone=$model->countByPhone($data['phone']);
        $this->ltValidate($countPhone,5,'该手机号码已超过账户绑定限制');
        
        $tui=$model->findUserByUsername($data['tuiname']);
        $this->notEmptyValidate($tui,'推荐人不存在');
        $this->neqValidate($tui['lock'],1,'推荐人账号被冻结');
        unset($data['tuiname']);
        $data['tui_id']=$tui['id'];
        
        $jie=$model->findUserByUsername($data['jiename']);
        $this->notEmptyValidate($jie,'接点人不存在');
        $this->neqValidate($jie['lock'],1,'接点人账号被冻结');
        unset($data['jiename']);
        $data['jie_id']=$jie['id'];
        if ($data['quyu']!='1') {
            $left_user=$model->findUserByJieIdAndQuyu($jie['id'], 1);
            $this->notEmptyValidate($left_user,'该用户左区还没有用户');
        }
        
        $check=$model->findUserByJieIdAndQuyu($jie['id'],$data['quyu']);
        $this->emptyValidate($check,'此区域已经有人');
        
        $currency=CurrencyModel::newSelf()->getInfo();
        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        
        $num=$leavel_type[$data['leavel']]['money'];
        
        $this->egtValidate($user_money[$need_currency]['num'],$num,'您的'.$currency[$need_currency]['name'].'不足');
        
        $data['password']=$model->passwordEncrypt('123456');
        $data['twopassword']=$model->twoPasswordEncrypt('123456');
        $data['reg_time']=time();
        $user_id=$model->add($data);
        
        $bank_data['user_id']=$user_id;
        $bank_data['add_time']=time();
        UserBankModel::newSelf()->add($bank_data);
        
        $money_model=UserMoneyModel::newSelf();
        $money_model->initializationMoneyInfo($user_id,$data['leavel']);

        $reward= Reward::newSelf($user_id,$num);
        
        $money_model->decUserMoney($this->home_user_info['id'], $need_currency, $num);
        
        $bonus_id=$reward->updateBonusLog($this->home_user_info['id'],['bonus5'=>$num]);
        FinanceLogModel::newSelf()->addLog(100, $num, $this->home_user_info['id'], $need_currency,$user_id,$bonus_id);
       
        $data['user_id']=$this->home_user_info['id'];
        $data['reg_type']=$need_currency;
        $data['num']=$num;
        $data['reg_id']=$user_id;
        $data['leavel']=$data['leavel'];
        $data['add_time']=time();
        $data['type']=1;
        RegLogModel::newSelf()->add($data);
        
        
//         $reward->anzhijiang();
        $reward->initialTree();
//         $reward->jiandianjiang();
//         $reward->duipeng();
        $reward->guanlijiang();
        $reward->tuijianjiang();
//         $user_money=$money_model->getUserMoneyInfo($user_id);
//         $this->tradeBuyBeginByMoney($user_id, $user_money['11']['num'],$currency['11'],$currency['1']);
        phoneCode($data['phone'],'reg',1);
       // {:U('Team/sendRegPhoneCode')}
        $this->resReturn($user_id, '注册成功', '注册失败');
        
    }
    public function upgrade()
    {
        $leavel_type=LeavelModel::newSelf()->getInfo();
        $this->assign('leavel_type',$leavel_type);

        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        $this->assign('user_money',$user_money);

        $currency=CurrencyModel::newSelf()->getInfo();
        $this->assign('currency',$currency);
        
        $this->display();
    }
    public function upgradeSub()
    {
        $data['leavel']=I('leavel');
        $twopassword=I('twopassword');
        $this->notEmptyValidate($twopassword,'请填写二级密码');
        
        $leavel_type=LeavelModel::newSelf()->getInfo();
        $this->notEmptyValidate($data['leavel'],'请选择会员等级');
        $this->notEmptyValidate($leavel_type[$data['leavel']],'请正确选择会员等级');

        $reg_type=(int)I('reg_type');
        $this->notEmptyValidate($reg_type,'请选择开通组合');
        $this->inArrayValidate($reg_type,['1','2'],'请选择开通组合2');
        if ($reg_type==1) {
            $need_currency=6;
        }
        if ($reg_type==2) {
            $need_currency=6;
            $need_currency2=7;
        }
        $this->notEmptyValidate($need_currency,'请选择开通组合');
        
        $need_currency=6;
        $this->gtValidate($data['leavel'], $this->home_user_info['leavel'],'您的等级高于此等级');
        
        $model=UserModel::newSelf();
        $check=$model->checkUserTwoPassword($twopassword, $this->home_user_info['twopassword']);
        $this->trueValidate($check,'二级密码不正确');

        $currency=CurrencyModel::newSelf()->getInfo();
        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        
        $num=$leavel_type[$data['leavel']]['money']-$leavel_type[$this->home_user_info['leavel']]['money'];
        ###
        if ($reg_type==1) {
            $this->egtValidate($user_money[$need_currency]['num'],$num,'您的'.$currency[$need_currency]['name'].'不足');
        }
        if ($reg_type==2) {
            $this->egtValidate($user_money[$need_currency]['num'],$num/2,'您的'.$currency[$need_currency]['name'].'不足');
            $this->egtValidate($user_money[$need_currency2]['num'],$num/2,'您的'.$currency[$need_currency2]['name'].'不足');
        }
        ###

        $money_model=UserMoneyModel::newSelf();
        $money_model->checkUpMoneyInfo($this->home_user_info['id'],$data['leavel'],$this->home_user_info['leavel']);

        if ($reg_type==1) {
            $money_model->decUserMoney($this->home_user_info['id'], $need_currency, $num);
            $reward= Reward::newSelf($this->home_user_info['id'],$num);
            $bonus_id=$reward->updateBonusLog($this->home_user_info['id'],['bonus5'=>$num]);
            FinanceLogModel::newSelf()->addLog(100, $num, $this->home_user_info['id'], $need_currency,$this->home_user_info['id'],$bonus_id);
        }
        if ($reg_type==2) {
            $money_model->decUserMoney($this->home_user_info['id'], $need_currency, $num/2);
            $money_model->decUserMoney($this->home_user_info['id'], $need_currency2, $num/2);
            $reward= Reward::newSelf($this->home_user_info['id'],$num);
            $bonus_id=$reward->updateBonusLog($this->home_user_info['id'],['bonus5'=>$num]);
            FinanceLogModel::newSelf()->addLog(100, $num/2, $this->home_user_info['id'], $need_currency,$this->home_user_info['id'],$bonus_id);
            FinanceLogModel::newSelf()->addLog(100, $num/2, $this->home_user_info['id'], $need_currency2,$this->home_user_info['id'],$bonus_id);
        }
        $data['user_id']=$this->home_user_info['id'];
        $data['reg_type']=$reg_type;
        $data['num']=$num;
        $data['reg_id']=$this->home_user_info['id'];
        $data['leavel']=$data['leavel'];
        $data['add_time']=time();
        $data['type']=2;
        RegLogModel::newSelf()->add($data);
        
        $reward= Reward::newSelf($this->home_user_info['id'],$num);
        $bonus_id=$reward->updateBonusLog($this->home_user_info['id'],['bonus5'=>$num]);
        FinanceLogModel::newSelf()->addLog(103, $num, $this->home_user_info['id'], $need_currency,$this->home_user_info['id'],$bonus_id);

        $res=$model->where(['id'=>$this->home_user_info['id']])->setField('leavel',$data['leavel']);
        $reward->tuijianjiang();
        $reward->anzhijiang();
        $reward->initialTree();
        $reward->duipeng();

        $this->resReturn($res, '升级成功', '升级失败');
        
    }
    
    
    public function pedigreeChart()
    {
        $user_id=$_SESSION['USER_KEY_ID'];
        if ($_GET['username']) {
            $username=I('username');
            $user=UserModel::newSelf()->findUserByUsername($username);
            $user_id=$user['id'];
        }
        $chart=UserModel::newSelf()->getPedigreeChartData($user_id,3);
        $this->assign('chart',$chart);
        $leavel_type=LeavelModel::newSelf()->getInfo();
        $this->assign('leavel_type',$leavel_type);
        $this->display();
    }
    public function pedigreeChart3()
    {
        $user_id=$_SESSION['USER_KEY_ID'];
        if ($_GET['username']) {
            $username=I('username');
            $user=UserModel::newSelf()->findUserByUsername($username);
            $user_id=$user['id'];
        }
        
        $chart=UserModel::newSelf()->getPedigreeChartData($user_id,3);
        $this->assign('chart',$chart);

        $leavel_type=LeavelModel::newSelf()->getInfo();
        $this->assign('leavel_type',$leavel_type);

        $this->display();
    }
    public function recommendTree()
    {
        $model=UserModel::newSelf();
        
  	    $this->assign('tuinum',$model->findUserTuiNum($_SESSION['USER_KEY_ID']));
  	    
  	    $this->assign('tree',$model->getTuiUserData($_SESSION['USER_KEY_ID']));
        
        $this->display();
    }
  	public function ajaxGetTuiUserData()
  	{
  	    $username=I('username');
  	    $user=UserModel::newSelf()->findUserByUsername($username);
  	    if (!$user)
  	    {
  	        $this->ajaxReturn(['status'=>false]);
  	    }
  	    $this->ajaxReturn(['status'=>true,'list'=>UserModel::newSelf()->getTuiUserData($user['id'])]);
  	}
    
    
    
    public function regLog()
    {
        $where['user_id']=$_SESSION['USER_KEY_ID'];
        $list=RegLogModel::newSelf()->pagingList($where);
        
        $leavel_type=LeavelModel::newSelf()->getInfo();
        $this->assign('leavel_type',$leavel_type);
        $this->assign('list',$list);
        $this->display();
    }

    public function sendRegPhoneCode(){
    	$phone=I('phone');
    	$a=phoneCode($phone,'reg');
    	$this->ajaxReturn($a);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}